<?php

use App\Admin\Controller\Base\RouteController;
use App\Admin\Controller\Base\SuperConfigController;
use App\Common\Enums\Base\SuperConfigEnumType;
use App\Common\Util\TP;
use Sc\Util\HtmlStructure\Html\Html;
use Sc\Util\HtmlStructure\Html\Js\JsCode;
use Sc\Util\HtmlStructure\Html\Js\JsIf;
use Sc\Util\HtmlStructure\Html\Js\JsVar;
use Sc\Util\HtmlStructure\Table;

Html::create('超级配置列表');

$table = Table::create(TP::route()->to([SuperConfigController::class, 'listsData'])->url(), 'super_config');

$table->addColumns(
    Table\Column::selection(),
    Table\Column::normal('配置的名称', 'name'),
    Table\Column::normal('配置的签名', 'sign')->addSearch('like'),
    Table\Column::normal('配置的类型', 'type')->showTag(SuperConfigEnumType::tagMapping())->addSearch('in'),
    Table\Column::normal('创建时间', 'create_time'),
);

$table->setHeaderEvent('@primary.Plus.新增',
    Table\EventHandler::window('新增')->setConfig(['width' => '1100px'])->setUrl( TP::route()->to([SuperConfigController::class, 'create']))
);

$table->setRowEvent('@primary.Edit.编辑',
    Table\EventHandler::window('编辑')->setConfig(['width' => '1100px'])->setUrl( TP::route()->to([SuperConfigController::class, 'edit']), ['id' => '@id'])
);
//'@primary.Setting.配置'
$table->setRowEvent('@primary.Setting.配置',
    JsCode::make(
        JsVar::def('width'),
        JsIf::when("row.type === 4 && window.innerWidth > 800")
            ->then(
                JsVar::assign('width', '90%')
            )->else(
                JsIf::when('row.type === 2 || row.type === 3')->then(
                    JsVar::assign('width', '600px'),
                )->else(
                    JsVar::assign('width', '1100px')
                )
            ),
        Table\EventHandler::window('配置')->setConfig(['width' => \Sc\Util\HtmlStructure\Html\Js\Grammar::mark("width")])
            ->setUrl( TP::route()->to([SuperConfigController::class, 'baseConfig'], ['id' => '@id'])),
    )
);
$appAlias = array_search(app()->http->getName(), TP::config('app.app_map'));
$table->setRowEvent('@primary.Plus.菜单',
    Table\EventHandler::window('加菜单')->setConfig(['width' => '600px'])->setUrl(
        TP::route()->to([RouteController::class, 'create'], [
            '_id' => '@id',
            'route' => substr(TP::route()->getRoute([SuperConfigController::class, 'baseConfig']), strlen($appAlias) + 1),
            'tag' => 'super_config'
        ])
    )
);

if (TP::isDev()) {
    $table->setHeaderEvent("@danger.delete.删除",
        Table\EventHandler::post(
            url: TP::route()->to([SuperConfigController::class, 'destroy']),
            data: ['ids' => '@selection.map(v => v.id)'],
            successHandler: JsCode::create("this.\$message.success('删除成功')")->then("this.super_configGetData()")
        )->confirmMessage('确认删除选中数据吗？')
            ->addLoading()
    );
}

Html::js()->vue->set('htmlHeight', '@window.innerHeight');

$table->setAttr(':height', 'htmlHeight - 150');

$table->addSearch(
    \Sc\Util\HtmlStructure\Form\FormItem::text('name')->placeholder('名称'),
    'like_right'
);

return $table->render();
